package com.powernode.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.powernode.constant.ProductConstants;
import com.powernode.domain.Category;
import com.powernode.ex.headler.BusinessException;
import com.powernode.mapper.CategoryMapper;
import com.powernode.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@CacheConfig(cacheNames = "com.powernode.service.impl.CategoryServiceImpl")
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {


    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 查询商品所有类目
     *
     * @return
     */
    @Override
    @Cacheable(key = ProductConstants.ALL_CATEGORY_LIST_KEY)
    public List<Category> queryAllCategoryList() {
        return categoryMapper.selectList(new LambdaQueryWrapper<Category>()
                .orderByDesc(Category::getSeq));
    }

    /**
     * 查询系统商品一级类目
     *
     * @return
     */
    @Override
    @Cacheable(key = ProductConstants.FIRST_CATEGORY_LIST_KEY)
    public List<Category> queryFirstCategoryList() {
        return categoryMapper.selectList(new LambdaQueryWrapper<Category>()
                .eq(Category::getParentId, 0)
                .eq(Category::getStatus, 1)
                .orderByDesc(Category::getSeq));
    }


    /**
     * 保存类目
     *
     * @param category 类目
     * @return
     */
    @Override
//    @CacheEvict(key = ProductConstants.ALL_CATEGORY_LIST_KEY)
//    @CacheEvict(key = ProductConstants.FIRST_CATEGORY_LIST_KEY)
    @Caching(evict = {
            @CacheEvict(key = ProductConstants.ALL_CATEGORY_LIST_KEY),
            @CacheEvict(key = ProductConstants.FIRST_CATEGORY_LIST_KEY)
    })
    public Boolean saveCategory(Category category) {
        category.setCreateTime(new Date());
        category.setUpdateTime(new Date());
        return categoryMapper.insert(category) > 0;
    }


    /**
     * 修改类目   允许修改商品类目级别
     * @param category
     * @return
     */
    @Override
    @Caching(evict = {
            @CacheEvict(key = ProductConstants.ALL_CATEGORY_LIST_KEY),
            @CacheEvict(key = ProductConstants.FIRST_CATEGORY_LIST_KEY)
    })
    public Boolean modifyCategory(Category category) {
//        根据标识查询类目详情
        Category beforeCategory = categoryMapper.selectById(category.getCategoryId());
//        判断商品类目修改情况
//        获取商品类目之前的级别 如果为0，则为一级类目，否则为二级类目
        Long beforeCategoryParentId = beforeCategory.getParentId();
//        1 > 2
        if (beforeCategoryParentId == 0 && category.getParentId() != null &&
                category.getParentId() != 0) {
//            查看当前类目是否包含子类目，如果包含子类目，则不允许修改
            List<Category> categoryList = categoryMapper.selectList(new LambdaQueryWrapper<Category>()
                    .eq(Category::getParentId, category.getCategoryId()));
            if (CollectionUtil.isNotEmpty(categoryList) && categoryList.size() != 0) {
//                 包含子类目，则不允许修改
                throw new BusinessException("当前类目包含子类目,不允许修改");
            }

        }
//        2 > 1
        if (beforeCategoryParentId != 0 && category.getParentId() == null) {
            category.setParentId(0L);
        }
        return categoryMapper.updateById(category) > 0;
    }


    /**
     * 删除类目 如果一级类目包含子类目，则不允许删除
     * @param categoryId
     * @return
     */
    @Caching(evict = {
            @CacheEvict(key = ProductConstants.ALL_CATEGORY_LIST_KEY),
            @CacheEvict(key = ProductConstants.FIRST_CATEGORY_LIST_KEY)
    })
    @Override
    public Boolean removeCategoryById(Long categoryId) {
//        根据类目标识查询子类目集合
        List<Category> categoryList = categoryMapper.selectList(new LambdaQueryWrapper<Category>()
                .eq(Category::getParentId, categoryId));
        if (CollectionUtil.isNotEmpty(categoryList) && categoryList.size() != 0) {
//            存在子类目，则不允许删除
            throw new BusinessException("当前类目包含子类目，不允许删除");
        }
        return categoryMapper.deleteById(categoryId) > 0;
    }
}
